package cn.cxq.learning.e6;

public class JordanElimination {

    int MM = 4;
    int MN = 5;
    int RN = MN;
    int RM = MM;
    double[][] A = new double[MM][MN];

    private double GetMainElement(int K) {
        int i, j, flag;
        double token, temp;
        flag = K;
        token = Math.abs(A[K][K]);
        for (i = K + 1; i < RM; i++) {
            if (Math.abs(A[i][K]) <= token) continue;
            flag = i;
            token = Math.abs(A[i][K]);
        }
        for (j = 0; j < RN; j++) {
            temp = A[K][j];
            A[K][j] = A[flag][j];
            A[flag][j] = temp;
        }
        return token;
    }

    public void Jordan() {
        int i, j, k;
        double x;

        for (k = 0; k < RM; k++) {
            double test;
            test = GetMainElement(k);
            if (test == 0) System.out.print("error");
            x = A[k][k];
            for (j = k; j < RN; j++) A[k][j] /= x;

            for (i = 0; i < RM; i++) {
                if (i == k) continue;
                x = A[i][k];
                for (j = k; j < RN; j++)
                    A[i][j] -= x * A[k][j];
            }

        }
    }
}
